import 'package:flutter/foundation.dart';

class AppEnv {
  @deprecated
  AppEnv();

  /// 默认的环境配置信息
  static EnvConfig? _defaultEnvConfig;

  /// 可做修改的环境配置信息
  static EnvConfig? _envConfig;

  /// 可做修改的图片基地址
  static String? _imgBaseURL;

  /// app桥接过来的配置信息
  static EnvConfig? app2Env;

  /// 当前的环境配置信息
  static EnvConfig get _currEnv {
    if (_envConfig == null) {
      if (_defaultEnvConfig == null) {
        final _type = _stringToEnvType(currCompilerIdentify);
        final EnvType type;
        if (kReleaseMode) {
          type = _type ?? EnvType.release;
        } else {
          type = _type ?? EnvType.develop;
        }
        _defaultEnvConfig = EnvConfig(env: type);
      }
      return _defaultEnvConfig!;
    }
    return _envConfig!;
  }

  /// 环境key
  static const envIdentityKey = 'FLUTTER_APP_ENV';

  /// 当前编译带入的环境标识
  static const currCompilerIdentify = String.fromEnvironment(envIdentityKey);

  /// 当前的环境标识
  static String get currEnvIdentify => _currEnv.identity;
  static EnvType get currEnvType => _currEnv.env;
  static String get apiBaseURL => _currEnv.apiBaseURL;
  static String get webBaseURL => _currEnv.webBaseURL;
  static String get h5BaseURL => _currEnv.h5BaseURL;
  static String get imgBaseURL => _imgBaseURL ?? _currEnv.imgBaseURL;
  static set imgBaseURL(String value) {
    _imgBaseURL = value;
  }

  static switchEnv(EnvConfig? envConfig) {
    _envConfig = envConfig;
  }
}

class EnvConfig {
  EnvConfig(
      {required this.env,
      String? apiBaseURL,
      String? webBaseURL,
      String? h5BaseURL,
      String? imgBaseURL}) {
    switch (env) {
      case EnvType.develop:
        this.apiBaseURL = 'http://192.168.168.5:7778';
        this.webBaseURL = 'http://192.168.168.5:9904';
        this.h5BaseURL = 'http://192.168.168.5:9903';
        this.imgBaseURL = 'http://192.168.168.5:8022';
        break;
      case EnvType.test:
        this.apiBaseURL = 'http://192.168.168.9:7778';
        this.webBaseURL = 'http://192.168.168.9:9904';
        this.h5BaseURL = 'http://192.168.168.9:9903';
        this.imgBaseURL = 'http://192.168.168.9:8022';
        break;
      case EnvType.uat:
        this.apiBaseURL = 'https://uatapi.taomz.com';
        this.webBaseURL = 'https://uat.taomz.com';
        this.h5BaseURL = 'https://uatsolve.taomz.com';
        this.imgBaseURL = 'https://uatimages.taomz.com';
        break;
      case EnvType.release:
        this.apiBaseURL = 'https://www.appapi.taomz.com';
        this.webBaseURL = 'https://www.taomz.com';
        this.h5BaseURL = 'https://www.solve.taomz.com';
        this.imgBaseURL = 'https://www.static.taomz.com';
        break;
      case EnvType.custom:
        this.apiBaseURL = apiBaseURL ?? AppEnv.apiBaseURL;
        this.webBaseURL = webBaseURL ?? AppEnv.webBaseURL;
        this.h5BaseURL = h5BaseURL ?? AppEnv.h5BaseURL;
        this.imgBaseURL = imgBaseURL ?? AppEnv.imgBaseURL;
        break;
    }
  }
  final EnvType env;
  String get identity => _envTypeToString(env);

  /// 图片服务基地址（可单独修改）
  late String imgBaseURL;

  /// 接口基地址（跟随环境变化）
  late String apiBaseURL;

  /// web页基地址（跟随环境变化）
  late String webBaseURL;

  /// h5混编基地址（跟随环境变化）
  late String h5BaseURL;

  bool operator ==(other) {
    return identical(this, other) &&
        other is EnvConfig &&
        other.env == env &&
        other.apiBaseURL == apiBaseURL &&
        other.webBaseURL == webBaseURL &&
        other.h5BaseURL == h5BaseURL;
  }

  @override
  int get hashCode =>
      env.hashCode ^
      apiBaseURL.hashCode ^
      webBaseURL.hashCode ^
      h5BaseURL.hashCode;
}

enum EnvType { develop, test, uat, release, custom }

EnvType? _stringToEnvType(String value) {
  switch (value) {
    case 'develop':
      return EnvType.develop;
    case 'test':
      return EnvType.test;
    case 'uat':
      return EnvType.uat;
    case 'release':
      return EnvType.release;
    case 'custom':
      return EnvType.custom;
    default:
      return null;
  }
}

String _envTypeToString(EnvType value) {
  switch (value) {
    case EnvType.develop:
      return 'develop';
    case EnvType.test:
      return 'test';
    case EnvType.uat:
      return 'uat';
    case EnvType.release:
      return 'release';
    case EnvType.custom:
      return 'custom';
  }
}
